package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.pojo.core.ChargeNavigatiorInfo;
import com.zhentao.model.pojo.core.param.ChargeNavigatorParam;
import com.zhentao.service.ChargeNavigatiorInfoService;
import com.zhentao.mapper.ChargeNavigatiorInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.zhentao.common.user.UserCommon.CHARGE_USER_LOGIN_KEY_SCHEDULE;

/**
* @author lenovo
* @description 针对表【charge_navigatior_info】的数据库操作Service实现
* @createDate 2024-12-25 21:17:04
*/
@Service
public class ChargeNavigatiorInfoServiceImpl extends ServiceImpl<ChargeNavigatiorInfoMapper, ChargeNavigatiorInfo>
    implements ChargeNavigatiorInfoService{
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * @description  添加导航信息
     * @author 振涛教育_小Q
     * @date    2024/12/25 21:19
     * @param params
     * @return com.zhentao.model.common.dtos.ResponseResult
     */
    @Override
    public ResponseResult insertNavigator(List<ChargeNavigatorParam> params) {
        // 获取当前登录用户
        String userId = redisTemplate.opsForValue().get(CHARGE_USER_LOGIN_KEY_SCHEDULE);
        if(StringUtils.checkValNull(userId)){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHARGE_USER_DATA_NOT_EXIST);
        }
        boolean flag = false;
        if(params != null && params.size() > 0){
            // 判断数据库中是否已经存在
            for (ChargeNavigatorParam param : params) {
                // 不存在 则添加
                ChargeNavigatiorInfo navigatiorInfo = getOne(Wrappers.<ChargeNavigatiorInfo>lambdaQuery()
                        .eq(StringUtils.checkValNotNull(param.getNavigatorName()),
                                ChargeNavigatiorInfo::getNavigatorName, param.getNavigatorName())
                        .eq(ChargeNavigatiorInfo::getUserId, Integer.valueOf(userId)));
                if(navigatiorInfo == null){
                    // 添加
                    ChargeNavigatiorInfo info = new ChargeNavigatiorInfo();
                    info.setNavigatorName(param.getNavigatorName());
                    info.setNavigatorLatitude(param.getNavigatorLatitude());
                    info.setNavigatorLongitude(param.getNavigatorLongitude());
                    info.setUserId(Long.parseLong(userId));
                    flag = save(info);
                }
            }
        }
        return flag?ResponseResult.okResult(AppHttpCodeEnum.SUCCESS):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    /**
     * @description  根据名称查询经纬度
     * @author 振涛教育_小Q
     * @date    2024/12/25 21:19
     * @param
     * @return com.zhentao.model.common.dtos.ResponseResult
     */
    @Override
    public ResponseResult selectNavigator(ChargeNavigatorParam params) {
        if(com.alibaba.cloud.commons.lang.StringUtils.isBlank(params.getNavigatorName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        ChargeNavigatiorInfo navigatiorInfo = null;
        try {
            // 根据名字查询
            navigatiorInfo = getOne(Wrappers.<ChargeNavigatiorInfo>lambdaQuery()
                    .eq(StringUtils.checkValNotNull(params.getNavigatorName()),
                            ChargeNavigatiorInfo::getNavigatorName, params.getNavigatorName()));
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return ResponseResult.okResult(navigatiorInfo);
    }

    /**
     * @description  查询导航点列表
     * @author 振涛教育_小Q
     * @date    2024/12/30 16:31
     * @param
     * @return com.zhentao.model.common.dtos.ResponseResult
     */
    @Override
    public ResponseResult navigationList() {
        // 获取当前登录用户
        String userId = redisTemplate.opsForValue().get(CHARGE_USER_LOGIN_KEY_SCHEDULE);
        if(StringUtils.checkValNull(userId)){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHARGE_USER_DATA_NOT_EXIST);
        }
        // 查询导航列表
        List<ChargeNavigatiorInfo> list = list(Wrappers.<ChargeNavigatiorInfo>lambdaQuery()
                .eq(ChargeNavigatiorInfo::getUserId,Integer.valueOf(userId))
                .orderByDesc(ChargeNavigatiorInfo::getCreateTime));
        return ResponseResult.okResult(list);
    }
}




